/*
class Stack{
	constructor() {
		this.items = []
	}
	push(element) {
		this.items.push(element)
	}
	pop() {
		return this.items.pop()
	}
	peek() {
		return this.items[this.items.length - 1]
	}
	isEmpty() {
		return this.items.length === 0
	}
	size() {
		return this.items.length
	}
	clear(){
		this.items = []
	}
}
const stack = new Stack();
console.log(stack.isEmpty())
stack.push(5)
stack.push(8)
console.log(stack.size())
console.log(stack.peek())
console.log(stack.pop())
console.log(stack.pop())*/

//创建一个基于JavaScript对象的stack类
class Stack{
	constructor() {
		this.count = 0;
		this.items = {}
	}
	push(element) {         //像栈中插入元素
		this.items[this.count] = element;       //count为items对象的键名
		this.count++        //加入后，count+1
	}
	size(){         //查看栈的长度
		return this.count
	}
	isEmpty() {         //查看栈是否为空
		return this.count === 0
	}
	pop(){     //弹出元素
		if(this.isEmpty()){ //栈为空则返回undefined
			return undefined
		}
		this.count--;    //不为空 则count-1
		const result = this.items[this.count];   //最上面元素的索引 保存方便返回
		delete this.items[this.count];          //删除栈顶元素
		return result                   //返回删除元素
	}

	peek(){               //查看栈顶元素
		if(this.isEmpty()){
			return undefined
		}
		return this.items[this.count - 1]    //count-1 为栈顶元素的索引
	}
	clear() {               //清空栈
		this.items = {};
		this.count = 0;

		// while(!isEmpty){
		// 	stack.pop()
		// }
	}
	toString(){ //创建一个toString方法
		if(this.isEmpty()){
			return ''
		}
		let objString = `${this.items[0]}`;     //最下面为第一个字符串
		for(let i = 1; i < this.count; i++){        //遍历栈
			objString = `${objString},${this.items[i]}` //拼接 ，隔开
		}
		return objString
	}


}
const stack = new Stack();
stack.push(5);
stack.push('9a');
stack.push('d');
// console.log(stack)
// stack.pop();
// console.log(stack)
console.log(stack.peek())
console.log(stack.toString())





